package dk.vighdata.slzapp;

import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends ActionBarActivity {
    private LanCounter lc;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timer = new Timer();

        TimerTask minuteTask = new TimerTask() {
            @Override
            public void run() {
                Handler refresh = new Handler(Looper.getMainLooper());
                refresh.post(new Runnable() {
                    public void run() {
                        setCounter();
                    }
                });
            }
        };
        timer.schedule(minuteTask, 0, 1000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void setCounter() {
        lc = new LanCounter(2015, 04, 02);
        TextView counterWeeks = (TextView) findViewById(R.id.txtWeeks);
        TextView counterDays = (TextView) findViewById(R.id.txtDays);
        TextView counterHours = (TextView) findViewById(R.id.txtHours);
        TextView counterMinutes = (TextView) findViewById(R.id.txtMinutes);
        TextView counterSeconds = (TextView) findViewById(R.id.txtSeconds);

        counterWeeks.setText(String.valueOf(lc.getWeeksToLan()) + " Uger");
        counterDays.setText(String.valueOf(lc.getDaysAfterWeeks()) + " Dage");
        counterHours.setText(String.valueOf(lc.getHoursAfterDays()) + " Timer");
        counterMinutes.setText((String.valueOf(lc.getMinutesAfterHours())) + " Minutter");
        counterSeconds.setText(String.valueOf(lc.getSecondsAfterMinutes()) + " Sekunder");
    }
}
